package com.imprenta.ejb;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.hibernate.util.HibernateUtil;
import com.imprenta.entities.Empleado;
import com.imprenta.entities.Login;
import com.imprenta.model.UsuarioBase;

public class EjbEmpleado {
	
	public static boolean SaveEmpleado(Empleado empleado, Login login){
		
		boolean success = false;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			Transaction transaction = session.beginTransaction();
			session.save(login);
			empleado.setLogin(login);
			session.save(empleado);
			transaction.commit();
			success = true;
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}
		catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			if (session != null) {
				session.close();
			}
		}
		return success;
	}//end savecempleado
	@SuppressWarnings("rawtypes")
	public static ArrayList<UsuarioBase> getListaEmpleado(){
		
		ArrayList<UsuarioBase> lstEmpleado = new ArrayList<UsuarioBase>();
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			String Qry = "select e.empleadoid, e.nombre ||' '|| e.apellido, l.username, l.password, l.idlogin from Empleado e inner join e.login l";
			Query q = session.createQuery(Qry);
			List l = q.list();
			for (int i = 0; i < l.size(); i++) {
				Object[] o = (Object[]) l.get(i);
				UsuarioBase ub = new UsuarioBase();
				ub.setEmpleadoId(Integer.parseInt(o[0].toString()));
				ub.setNombreCompleto(o[1].toString());
				ub.setEmpleadoUser(o[2].toString());
				ub.setContrasenia(o[3].toString());
				ub.setLoginId(Integer.parseInt(o[4].toString()));
				
				lstEmpleado.add(ub);				
			}
	
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if (session != null) {
				session.close();
			}			
		}
		return lstEmpleado;
	}//fin getlistaempleado
	
	public static boolean updateLoginEmpleado(Login l){
		boolean update = false;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			Transaction transaction = session.beginTransaction();
			session.saveOrUpdate(l);
			transaction.commit();
			update = true;
		} catch (HibernateException he) {
			// TODO: handle exception
			update = false;
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			update = false;
			e.printStackTrace();
		}finally{
			if (session != null) {
				session.close();
			}
		}
		return update;
	}//fin updateloginempleado
	
@SuppressWarnings("rawtypes")
public static Login GetLogin(int _id){
		
		Login l = new Login();
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			String query = 	"select l.idlogin, l.username,l.password,l.rol from Login l inner join l.empleados e where e.empleadoid = :id";
			Query q = session.createQuery(query);
			q.setParameter("id",_id);
			q.setMaxResults(1);
			List lista = q.list();
			
			for (int i = 0; i < lista.size(); i++) {
				Object[] items = (Object[])lista.get(i);
				
				l.setIdlogin(Integer.parseInt(items[0].toString()));
				l.setUsername(items[1].toString());
				l.setPassword(items[2].toString());
				l.setRol(items[3].toString());
			}
			
		} catch (HibernateException he) {
			// TODO: handle exception
			he.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			if (session != null) {
				session.close();
			}
		}
		return l;
	}//end getlogin

}
